package com.zhou.jianzhi.controller.web;/**
 * @project jianzhi-server-master
 * @description
 * @author 黄嘉杰
 * @date 2025/1/22 09:56:48
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhou.jianzhi.common.AjaxResult;
import com.zhou.jianzhi.config.annotation.GuestAccess;
import com.zhou.jianzhi.entity.dto.BaseUserDTO;
import com.zhou.jianzhi.entity.po.BaseUser;
import com.zhou.jianzhi.entity.po.JobSeeker;
import com.zhou.jianzhi.entity.po.UnlikeMessage;
import com.zhou.jianzhi.mapper.BaseUserMapper;
import com.zhou.jianzhi.mapper.JobSeekerMapper;
import com.zhou.jianzhi.mapper.UnlikeMessageMapper;
import com.zhou.jianzhi.service.IJobSeekerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author 黄嘉杰
 * @date 2025/1/22 9:56
 * @version 1.0
 */
@Controller
@RequestMapping("/unlike")
public class UnlikeController {

    @Autowired
    private UnlikeMessageMapper unlikeMessageMapper;

    @Autowired
    private IJobSeekerService iJobSeekerService;

    @Autowired
    private JobSeekerMapper mapper;


    @ResponseBody
    @GetMapping("/get")
    @GuestAccess
    public AjaxResult getList(@RequestParam(value = "username", required = false) String username) {
        if (StringUtils.isEmpty(username)) {
            return AjaxResult.success( unlikeMessageMapper.selectList(null));
        }
        QueryWrapper<JobSeeker> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", username);
        JobSeeker jobSeekers = mapper.selectOne(queryWrapper);
        Integer id = jobSeekers.getId();
        QueryWrapper<UnlikeMessage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("job_seeker_id", id);
        List<UnlikeMessage> unlikeMessages = unlikeMessageMapper.selectList(queryWrapper1);
        return AjaxResult.success(unlikeMessages);
    }

    /**
     * 获取所有意见列表zhou
     *
     * @param
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @GuestAccess
    public AjaxResult getList() {
        List<UnlikeMessage> unlikeMessages = unlikeMessageMapper.selectList(null);

        return AjaxResult.success(unlikeMessages);
    }

    /**
     * 新增意见
     *
     * @param unlikeMessage
     * @return
     */
    @ResponseBody
    @PostMapping ("/add")
    @GuestAccess
    public AjaxResult add(@RequestBody UnlikeMessage unlikeMessage) {
        Integer userId = unlikeMessage.getJobSeekerId();
        QueryWrapper<JobSeeker> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        JobSeeker jobSeeker = mapper.selectOne(queryWrapper);
        unlikeMessage.setCreator(jobSeeker.getName());
        int insert = unlikeMessageMapper.insert(unlikeMessage);
        if (insert > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 根据id删除意见
     *
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/delete/{id}")
    @GuestAccess
    public AjaxResult delete(@PathVariable Integer id) {
        int i = unlikeMessageMapper.deleteById(id);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ResponseBody
    @GetMapping("/getBy/{id}")
    @GuestAccess
    public AjaxResult getByID(@PathVariable Integer id) {
        UnlikeMessage unlikeMessage = unlikeMessageMapper.selectById(id);
        String content = unlikeMessage.getContent();
        return AjaxResult.success(content);
    }

}
